package com.hand.test.interceptor;/**
 * @author Estelle@앙수청
 * @date 2019/7/20 16:03
 */

import com.hand.test.annotation.Access;
import com.hand.test.annotation.PermissionAnnotation;
import com.hand.test.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.invoke.MethodHandle;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

/**
 *  @author Estelle@앙수청
 *  @date 2019/7/20 16:03
 */

public class ControllerInterceptor implements HandlerInterceptor {

    @Autowired
    private PermissionService permissionService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        boolean validate = false;

        String userId = request.getHeader("userId");
        List<Map<Integer, String>> maps = permissionService.queryUserPermission(Integer.valueOf(userId));
        // 取出方法
        HandlerMethod handlerMethod = (HandlerMethod)handler;
        Method method = handlerMethod.getMethod();
        // 取出注解值
        PermissionAnnotation annotation = method.getAnnotation(PermissionAnnotation.class);
        Access value = annotation.value();
        String permission = request.getMethod();

        AntPathMatcher matcher = new AntPathMatcher();
        for (Map<Integer, String> map : maps) {
            // TODO  枚举对应整数
//            matcher.match()
        }


        return validate;
    }
}
